#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "../DEMURA/Demura.h"
#include "usbd_cdc.h"

//********************************************************************************************
   

void RM692Ax_Enable_Flash_Control_and_SPI_Flash_En(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u16 tmp=0;
	u8 buffer1[3];	
	u8 RM692A9_Enable_Flash_Control[26] = {0xfe,0x26,0x04,0xcf,0x10,0x4b,0x14,0x22,0x1e,0x31,0xfe,0x20,0x5f,0x01,0x09,0x13,0xfe,0x26,0xf3,0x02,0x18,0x13,0x19,0x2F,0x1A,0xAA};
	u8 RM692A9_Enable_Flash_Read_REG[3] = {0xEA,0xEB,0xEC};	
//	u8 RM69350_Enable_Flash_Control[24] = {0xfe,0xd2,0x01,0xcf,0xfe,0x40,0x4d,0x20,0xfe,0xd2,0x05,0x18,0xfe,0x20,0xf3,0x08,0xf3,0x00,0xf3,0x04,0xf3,0x02,0xfe,0xe0};
//	u8 RM69350_Enable_Flash_Read_REG[3] = {0x6d,0x6e,0x6f};	
	u8 RM692AX_Enable_Flash_Control_6088[26] = {0xfe,0x26,0x04,0xcf,0x10,0x4b,0x14,0x22,0x1e,0x31,0xfe,0x20,0x5f,0x01,0x09,0x13,0xfe,0x26,0xf3,0x02,0x18,0x13,0x19,0x48,0x1A,0xF8};
	u8 RM692AX_Enable_Flash_Control_647KPQ[26] = {0xfe, 0x26, 0xf3, 0x02, 0x18, 0x1C, 0x19, 0xEC, 0x1A, 0xF0};
//	
//	u8 RM692AX_Enable_Flash_Control_635[26] = {0xfe,0x26,0x04,0xcf,0x10,0x4b,0x14,0x22,0x1e,0x31,0xfe,0x20,0x5f,0x01,0x09,0x13,0xfe,0x26,0xf3,0x02,0x18,0x13,0x19,0x23,0x1A,0x00};
	if (buffer[3] == 0x9)//GM8A0_RM692C9
	{
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);
		for(i=0;i<sizeof(RM692A9_Enable_Flash_Control);i++)
		{
			buffer1[0] = 0x02;
			buffer1[1] = RM692A9_Enable_Flash_Control[i];
			buffer1[2] = RM692A9_Enable_Flash_Control[i+1];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(5);
			i+=1;
		}
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
		SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
		for( i = 0; i< 3;i++)			 	
		{  	
				SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package                           
				buffer1[0] = 0x01;
				buffer1[1] = RM692A9_Enable_Flash_Read_REG[i];
				SSD2828_W_Array(SigMode,channel,buffer1,0); 
				delay_ms(6);
				buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);        
		}

		buffer[0] = 0x2F;
		buffer[1] = 0x06;		

		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);    
	}
	else// if((buffer[3] == 0x11)|(buffer[3]==0x12))	//641-D2 /M2/ 6.088  
	{
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);
		switch (buffer[3])
		{	case 0x12:
				{
					for(i=0;i<sizeof(RM692AX_Enable_Flash_Control_6088);i++)
					{
						buffer1[0] = 0x02;
						buffer1[1] = RM692AX_Enable_Flash_Control_6088[i];
						buffer1[2] = RM692AX_Enable_Flash_Control_6088[i+1];
						SSD2828_W_Array(SigMode,channel,buffer1,0);
						delay_ms(5);
						i+=1;
					}
				}break;
			case 0x11:
			default:
				{
					for(i=0;i<sizeof(RM692AX_Enable_Flash_Control_647KPQ);i++)
					{
						buffer1[0] = 0x02;
						buffer1[1] = RM692AX_Enable_Flash_Control_647KPQ[i];
						buffer1[2] = RM692AX_Enable_Flash_Control_647KPQ[i+1];
						SSD2828_W_Array(SigMode,channel,buffer1,0);
						delay_ms(15);
						i+=1;
					}
				}break;			
			
		}
	    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
	    SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
	    for( i = 0; i< 3;i++)			 	
	    {  	
	        SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package                           
	        buffer1[0] = 0x01;
	        buffer1[1] = RM692A9_Enable_Flash_Read_REG[i];
	        SSD2828_W_Array(SigMode,channel,buffer1,0); 
					delay_ms(6);
	        buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);        
	    }	
			//buffer[2]=5;
		delay_ms(5);
	    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回
	}	
	
}

void RM692Ax_Inter_Demura_Ram(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];
	u8 RM692A9_Inter_Demura_Ram_Cmd_Table[28]={0xfe,0xA0,0x0B,0x11,0xfe,0xd0,0x58,0x53,0x07,0x8D,0x42,0x81,0x55,0x58,0xFE,0xD2,0x6E,0x20,0xFE,0x40,0x5B,0x01,0x5E,0x40,0x0E,0x40,0xfe,0x00};		
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
		
	if((buffer[3]==9)|(buffer[3]==0x11)|(buffer[3]==0x12))	
	{for(i=0;i<sizeof(RM692A9_Inter_Demura_Ram_Cmd_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM692A9_Inter_Demura_Ram_Cmd_Table[i];
		buffer1[2] = RM692A9_Inter_Demura_Ram_Cmd_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(15);
		i+=1;
	}	
	buffer[0] = 0x2F;
	buffer[1] = 0x01;		

	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);    
	}	
	else if (buffer[3]==0x11)
	{
	}
}

                                    
void RM692Ax_Exit_Demura_Ram(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}
                               
void RM692Ax_Flash_Write_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u32 error_times=0;
	u8 buffer1[3];
	u8 RM692A9_Flash_Write_Demura_data_Table[14]={0xfe,0x26,0x1B,0x00,0xF2,0x04,0x15,0x00,0x16,0x00,0x17,0x00,0xF2,0x10};
	
	delay_ms(2500);//烧录前强制等待1.5s
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);	
	
	//if ((buffer[3]==0x11)|(buffer[3]==0x12)|(buffer[3]==9))
	{
		for(i=0;i<sizeof(RM692A9_Flash_Write_Demura_data_Table);i++)
		{
			buffer1[0] = 0x02;
			buffer1[1] = RM692A9_Flash_Write_Demura_data_Table[i];
			buffer1[2] = RM692A9_Flash_Write_Demura_data_Table[i+1];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(5);
			i+=1;
		}	
		delay_ms(2000);
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
		SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); //return package  
		do
		{                        
				buffer1[0] = 0x01;
				buffer1[1] = 0xDB;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 
				delay_ms(6);
				buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);   
				delay_ms(100);
		}while((buffer[4]&0x80)!=0x80);
		delay_ms(5);
		buffer[0] = 0x2F;
		buffer[1] = 0x03;		
		buffer[4] = 0x80;	
			
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);	
	}
		
}                       


void RM692Ax_Flash_Check_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u16 tmp=0;
	u8 buffer1[3];
	u8 RM692A9_Flash_Check_Read_REG[2]={0xE6,0xE7};
	u8 RM692A9_Flash_Check_Demura_data_Table_GM8A0[10]={0xfe,0x26,0x18,0x13,0x19,0x2F,0x1A,0xAA,0xF2,0x01};
	//u8 RM692A9_Flash_Check_Demura_data_Table[10]=      {0xfe,0x26,0x18,0x13,0x19,0x48,0x1A,0xF4,0xF2,0x01};
	u8 RM692A9_Flash_Check_Demura_data_Table_641_D2[10]={0xfe,0x26,0x18,0x13,0x19,0x48,0x1A,0xF4,0xF2,0x01};
		
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
	if (buffer[3]==0x09) //635 GM8A0
	{
		for(i=0;i<sizeof(RM692A9_Flash_Check_Demura_data_Table_GM8A0);i++)
		{
			buffer1[0] = 0x02;
			buffer1[1] = RM692A9_Flash_Check_Demura_data_Table_GM8A0[i];
			buffer1[2] = RM692A9_Flash_Check_Demura_data_Table_GM8A0[i+1];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(5);
			i+=1;
		}	
		delay_ms(500);
	
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
		SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);		
		for( i = 0;i<sizeof(RM692A9_Flash_Check_Read_REG);i++)			 	
		{  	
			SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package                           
			buffer1[0] = 0x01;
			buffer1[1] = RM692A9_Flash_Check_Read_REG[i];
			SSD2828_W_Array(SigMode,channel,buffer1,0); 
					delay_ms(6);
			buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);        
		}			
		  delay_ms(5);
			buffer[0] = 0x2F;
			buffer[1] = 0x04;					
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);   
	}
		 
	else if((buffer[3] == 0x11)|(buffer[3]==0x12)|(buffer[3]==0x13))	//RM692A9_641滴兔 M2--
	{
		for(i=0;i<sizeof(RM692A9_Flash_Check_Demura_data_Table_641_D2);i++)
		{
			buffer1[0] = 0x02;
			buffer1[1] = RM692A9_Flash_Check_Demura_data_Table_641_D2[i];
			buffer1[2] = RM692A9_Flash_Check_Demura_data_Table_641_D2[i+1];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(5);
			i+=1;
		}	
		delay_ms(500);
	
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
		SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);		
		for( i = 0;i<sizeof(RM692A9_Flash_Check_Read_REG);i++)			 	
		{  	
			SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package                           
			buffer1[0] = 0x01;
			buffer1[1] = RM692A9_Flash_Check_Read_REG[i];
			SSD2828_W_Array(SigMode,channel,buffer1,0); 
			delay_ms(6);
			buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);        
		}	
		delay_ms(5);
		buffer[0] = 0x2F;
		buffer[1] = 0x04;				

		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);			 //返回check id 值		
	}	
		
}
                                  
void RM692Ax_Flash_Erase_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];
	u8 RM692A9_Flash_Erase_Demura_data_Table[8]={0xfe,0x26,0x1B,0x00,0xF2,0x04,0xf3,0x01};

	u8 RM692A9_Read_REG=0xDB;	
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
	
	for(i=0;i<sizeof(RM692A9_Flash_Erase_Demura_data_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM692A9_Flash_Erase_Demura_data_Table[i];
		buffer1[2] = RM692A9_Flash_Erase_Demura_data_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		i+=1;
	}
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
	SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
	SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); //return package  
	do
	{                        
			buffer1[0] = 0x01;
			buffer1[1] = RM692A9_Read_REG;
			SSD2828_W_Array(SigMode,channel,buffer1,0); 
			delay_ms(6);
			buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);      
	}while((buffer[4]&0x80)!=0x80);
	delay_ms(5);
	buffer[0] = 0x2F;
	buffer[1] = 0x05;		
	buffer[4] = 0x80;	
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);          
}

void RM692Ax_Flash_Demura_OTP(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}

void RM692Ax_Demura_Function_ON(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[7];
	u8 RM692A9_Demura_Function_ON_Table[4] = {0xfe, 0x22, 0x77, 0x00};	
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);	
//	if (buffer[3]==9)
	{
		for(i=0;i<sizeof(RM692A9_Demura_Function_ON_Table);i++)
		{
			buffer1[0] = 0x02;
			buffer1[1] = RM692A9_Demura_Function_ON_Table[i];
			buffer1[2] = RM692A9_Demura_Function_ON_Table[i+1];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(5);
			i+=1;
		}
		delay_ms(5);
		buffer[0] = 0x2F;
		buffer[1] = 0x07;		
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);  
	}  
//	else if (buffer[3]==0x11)
//	{
//		for(i=0;i<sizeof(RM692A9_Demura_Function_ON_Table);i++)
//		{
//			buffer1[0] = 0x02;
//			buffer1[1] = RM692A9_Demura_Function_ON_Table[i];
//			buffer1[2] = RM692A9_Demura_Function_ON_Table[i+1];
//			SSD2828_W_Array(SigMode,channel,buffer1,0);
//			delay_ms(5);
//			i+=1;
//		}
//	
//		delay_ms(5);
//			buffer[0] = 0x2F;
//			buffer[1] = 0x07;		
//		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
//		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //数据长度-crc两字节
//	}
}

void RM692Ax_Demura_Function_OFF(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[7];
	u8 RM692A9_Demura_Function_OFF_Table[4] = {0xfe, 0x22, 0x77, 0x02};	
	u8 RM692A9_Demura_Function_OFF_Table_641_D2[4] = {0xfe, 0x22, 0x77, 0x04};	 //641 滴兔使用
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
	if ((buffer[3]==9)|(buffer[3]==0x13))  //2CX  系列用： M2、小明的8A0
	{
		for(i=0;i<sizeof(RM692A9_Demura_Function_OFF_Table);i++)
		{
			buffer1[0] = 0x02;
			buffer1[1] = RM692A9_Demura_Function_OFF_Table[i];
			buffer1[2] = RM692A9_Demura_Function_OFF_Table[i+1];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(5);
			i+=1;
		}

		delay_ms(5);
		buffer[0] = 0x2F;  //Demura_type
		buffer[1] = 0x08;				
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);  
	}  
	else //if ((buffer[3]==0x11)|(buffer[3]==0x12))		//2Ax 系列用
	{
		for(i=0;i<sizeof(RM692A9_Demura_Function_OFF_Table_641_D2);i++)
		{
			buffer1[0] = 0x02;
			buffer1[1] = RM692A9_Demura_Function_OFF_Table_641_D2[i];
			buffer1[2] = RM692A9_Demura_Function_OFF_Table_641_D2[i+1];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(5);
			i+=1;
		}

		delay_ms(5);
		buffer[0] = 0x2F;  //Demura_type
		buffer[1] = 0x08;				
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);  
	}       
}

void ProcessForDmuR09( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	switch(USB_Rx_Buffer[1])
	{
			case 0x01:                                      // 
					RM692Ax_Inter_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x02:                                      // 
					RM692Ax_Exit_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;						 
			case 0x03:                                      // 
					RM692Ax_Flash_Write_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x04:                                      // 
					RM692Ax_Flash_Check_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;												
			case 0x05:                                      // 
					RM692Ax_Flash_Erase_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			case 0x06:                                      // 
					RM692Ax_Enable_Flash_Control_and_SPI_Flash_En(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x07:                                      // 
					RM692Ax_Demura_Function_ON(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x08:                                      // 
					RM692Ax_Demura_Function_OFF(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;													
			
			default:
				break;												
	}
}
//********************************************************************************************
